package com.gin.kotlin.gf2gamecommunity.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedLiveData
import com.gin.kotlin.response.Res
import com.gin.kotlin.response.user.UserInfo

/**
 * @property cUser 当前用户信息
 * @constructor
 */
class UserViewModel : ViewModel() {
    init {
        Log.i(TAG, "实例化")
    }

    val currentUid = MutableLiveData<Int>()

    val cUser = CachedLiveData<Res<UserInfo.Body>, UserInfo>(
        requestMethod = { App.INSTANCE.api.userApi.getMemberInfo() },
        bodyTransform = { it.data?.user },
    )

    fun fetchCurrentUserInfo(force: Boolean = App.INSTANCE.currentAccount()?.uid != currentUid.value) = cUser.fetch(force) {
        currentUid.value = it.uid
    }

    companion object {
        private val TAG = UserViewModel::class.java.simpleName
    }
}